package br.com.massai.model.entity;

import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name = "UNIT_MEASURE")
@NamedQueries({
	@NamedQuery(name = "UnitMeasure.find", query = "SELECT u FROM UnitMeasure u"),
	@NamedQuery(name = "UnitMeasure.count", query = "SELECT COUNT(u) FROM UnitMeasure u"),
	@NamedQuery(name = "UnitMeasure.duplicate", query = "SELECT COUNT(u) FROM UnitMeasure u WHERE LOWER(u.acronym) = LOWER(:acronym) OR LOWER(u.nameMeasure) = LOWER(:nameMeasure)")
})
public class UnitMeasure extends GenericEntity {

	private static final long serialVersionUID = -6230331798098688517L;
	
	@Id
	@SequenceGenerator(name = "seqUnitMeasure", sequenceName = "seq_unit_measure", allocationSize = 1)
	@GeneratedValue(generator = "seqUnitMeasure", strategy = GenerationType.SEQUENCE)
	private Long id;
	
	@Column(name = "ACRONYM", length = 10, unique = true, nullable = false, columnDefinition = "VARCHAR(10)")
	private String acronym;
	
	@Column(name = "NAME_MEASURE", length = 100, unique = true, nullable = false, columnDefinition = "VARCHAR(100)")
	private String nameMeasure;
	
	@Override
	public Map<String, Object> getParameters() {
		super.clearParameters();

		if(this.acronym != null && !this.acronym.isEmpty()){
			super.addClauseWhere("LOWER(u.acronym) = LOWER(:acronym)", ConditionType.AND);
			super.addParameters("acronym", this.acronym);
		}

		if(this.nameMeasure != null && !this.nameMeasure.isEmpty()){
			super.addClauseWhere("LOWER(u.nameMeasure) LIKE LOWER(:nameMeasure)", ConditionType.AND);
			super.addParameters("nameMeasure", "%" + this.nameMeasure + "%");
		}

		return super.getParameters();
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj == null) return false;

		if(obj instanceof UnitMeasure)
			if(((UnitMeasure)obj).getId().equals(this.getId())) return true;

		return false;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getAcronym() {
		return acronym;
	}

	public void setAcronym(String acronym) {
		this.acronym = acronym;
	}

	public String getNameMeasure() {
		return nameMeasure;
	}

	public void setNameMeasure(String nameMeasure) {
		this.nameMeasure = nameMeasure;
	}
}
